﻿Imports System.Data
Imports ADOEntityKYC
Imports System.Xml
Imports ADOEntity
Imports System.Data.Objects.SqlClient


Partial Class KYC_AMLList
    Inherits System.Web.UI.Page

#Region " Variable Gobal "
    Dim ds As DataSet
    Dim clsAMLList As clsAMLList = New clsAMLList
    Dim TranType As String
    Dim uniqueID1 As String
    Dim passportID As String
    Dim hdfUniqueID As String
    Dim UniqueType As String
    Dim OtherDocID As String
    Dim corperateID As String
    Dim taxID As String
    Dim PrefixTH As String
    Dim EntityType As String
    Dim NameTh1 As String
    Dim NameTh2 As String
    Dim NameTh3 As String
    Dim PrefixEN As String
    Dim NameEn1 As String
    Dim NameEn2 As String
    Dim NameEn3 As String
    Dim Nationality As String
    Dim Occupation1 As String
    Dim OccOther1 As String
    Dim Business1 As String
    Dim BizOther1 As String
    Dim Des1 As String
    Dim Occupation2 As String
    Dim OccOther2 As String
    Dim Business2 As String
    Dim BizOther2 As String
    Dim Des2 As String
    Dim Occupation3 As String
    Dim OccOther3 As String
    Dim Business3 As String
    Dim BizOther3 As String
    Dim Des3 As String
    Dim IncomeSource1 As String
    Dim IncomeSource2 As String
    Dim IncomeSource3 As String
    Dim clsCheckLanguage As clsCheckLanguage
    Dim ListEntity As ListEntities = New ListEntities
    Dim clsDataTable As New clsLINQToDataTable
    Dim clsCheckID As clsCheckID
    Dim convertDate As clsDateFormat
    Dim clsDatabase As New clsDatabase
    Dim riskLv As String

    Dim splitIDNum As String()
    Dim splitIDPass As String()
    Dim splitIDOtherDoc As String()
    Dim splitCorpID As String()
    Dim splitTaxIDNumber As String()
    'Dim TypeKYC As String
    'Dim CustSeq As String
    Dim msgErr As String


#End Region


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            ds = Session("ds")
            clsAMLList = Session("clsAMLList")

            If Not IsPostBack Then

                QueryString()
                If Session("chkKYC") = Nothing Then
                    If checkBeforeCompile() = False Then
                        Session("chkKYC") = False

                        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "alert('" & msgErr & "'); window.close();", True)
                    Else
                        Session("chkKYC") = True
                        AutoInput()
                        CreateControlMP()
                        'QuerySanction() 'สิ่งที่ต้องแก้เป็น web service
                        'QuerySanction2() ' Use read from AMLO_NEW_TEMP DB : Fuzzy
                        QuerySanction3() ' Use read from AMLO_NEW_TEMP DB : Like

                        If ds.Tables("Result").Rows.Count = 0 Then
                            lblMsg.Visible = True
                            rdoSanction.Visible = False
                            btnOK.Enabled = True
                        Else
                            rdoSanction.Visible = True
                            grdList.Enabled = False
                        End If
                    End If
                Else
                    ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.close();", True)
                End If


            End If
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)

        End Try
    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Session("ds") = ds
    End Sub


#Region "SetForm"

    Sub CreateControlMP()
        Dim page As Label = CType(Master.FindControl("lblPage"), Label)
        page.Text = "ตรวจสอบ List"
    End Sub

#End Region

#Region "Function"

    Private Sub QueryString()
        Try
            '==== New Version Ball V-Smart=====
            hdfUniqueID = Request.QueryString("hdfUniqueID")
            hdfUniqueID = If(String.IsNullOrEmpty(hdfUniqueID), "", hdfUniqueID.Trim)
            uniqueID1 = Request.QueryString("uniqueID") ' Use as citizen Identity number
            uniqueID1 = If(String.IsNullOrEmpty(uniqueID1), "", uniqueID1.Trim)
            passportID = Request.QueryString("passportID") ' Use as passport number
            passportID = If(String.IsNullOrEmpty(passportID), "", passportID.Trim)
            TranType = Request.QueryString("TranType")
            TranType = If(String.IsNullOrEmpty(TranType), "", TranType.Trim)
            UniqueType = Request.QueryString("UniqueType")
            UniqueType = If(String.IsNullOrEmpty(UniqueType), "", UniqueType.Trim)
            PrefixTH = Request.QueryString("PrefixTH")
            PrefixTH = If(String.IsNullOrEmpty(PrefixTH), "", PrefixTH.Trim)
            NameTh1 = Request.QueryString("NameTh1")
            NameTh1 = If(String.IsNullOrEmpty(NameTh1), "", NameTh1.Trim)
            NameTh2 = Request.QueryString("NameTh2")
            NameTh2 = If(String.IsNullOrEmpty(NameTh2), "", NameTh2.Trim)
            NameTh3 = Request.QueryString("NameTh3")
            NameTh3 = If(String.IsNullOrEmpty(NameTh3), "", NameTh3.Trim)
            PrefixEN = Request.QueryString("PrefixEN")
            PrefixEN = If(String.IsNullOrEmpty(PrefixEN), "", PrefixEN.Trim)
            NameEn1 = Request.QueryString("NameEn1")
            NameEn1 = If(String.IsNullOrEmpty(NameEn1), "", NameEn1.Trim)
            NameEn2 = Request.QueryString("NameEn2")
            NameEn2 = If(String.IsNullOrEmpty(NameEn2), "", NameEn2.Trim)
            NameEn3 = Request.QueryString("NameEn3")
            NameEn3 = If(String.IsNullOrEmpty(NameEn3), "", NameEn3.Trim)
            Nationality = Request.QueryString("Nationality")
            Nationality = If(String.IsNullOrEmpty(Nationality), "", Nationality.Trim)
            Occupation1 = Request.QueryString("Occupation1")
            Occupation1 = If(String.IsNullOrEmpty(Occupation1), "", Occupation1.Trim)
            OccOther1 = Request.QueryString("OccOther1")
            OccOther1 = If(String.IsNullOrEmpty(OccOther1), "", OccOther1.Trim)
            Business1 = Request.QueryString("Business1")
            Business1 = If(String.IsNullOrEmpty(Business1), "", Business1.Trim)
            BizOther1 = Request.QueryString("BizOther1")
            BizOther1 = If(String.IsNullOrEmpty(BizOther1), "", BizOther1.Trim)
            Des1 = Request.QueryString("Des1")
            Des1 = If(String.IsNullOrEmpty(Des1), "", Des1.Trim)
            Occupation2 = Request.QueryString("Occupation2")
            Occupation2 = If(String.IsNullOrEmpty(Occupation2), "", Occupation2.Trim)
            OccOther2 = Request.QueryString("OccOther2")
            OccOther2 = If(String.IsNullOrEmpty(OccOther2), "", OccOther2.Trim)
            Business2 = Request.QueryString("Business2")
            Business2 = If(String.IsNullOrEmpty(Business2), "", Business2.Trim)
            BizOther2 = Request.QueryString("BizOther2")
            BizOther2 = If(String.IsNullOrEmpty(BizOther2), "", BizOther2.Trim)
            Des2 = Request.QueryString("Des2")
            Des2 = If(String.IsNullOrEmpty(Des2), "", Des2.Trim)
            Occupation3 = Request.QueryString("Occupation3")
            Occupation3 = If(String.IsNullOrEmpty(Occupation3), "", Occupation3.Trim)
            OccOther3 = Request.QueryString("OccOther3")
            OccOther3 = If(String.IsNullOrEmpty(OccOther3), "", OccOther3.Trim)
            Business3 = Request.QueryString("Business3")
            Business3 = If(String.IsNullOrEmpty(Business3), "", Business3.Trim)
            BizOther3 = Request.QueryString("BizOther3")
            BizOther3 = If(String.IsNullOrEmpty(BizOther3), "", BizOther3.Trim)
            Des3 = Request.QueryString("Des3")
            Des3 = If(String.IsNullOrEmpty(Des3), "", Des3.Trim)

            IncomeSource1 = Request.QueryString("IncomeSource1")
            IncomeSource1 = If(String.IsNullOrEmpty(IncomeSource1), "", IncomeSource1.Trim)
            IncomeSource2 = Request.QueryString("IncomeSource2")
            IncomeSource2 = If(String.IsNullOrEmpty(IncomeSource2), "", IncomeSource2.Trim)
            IncomeSource3 = Request.QueryString("IncomeSource3")
            IncomeSource3 = If(String.IsNullOrEmpty(IncomeSource3), "", IncomeSource3.Trim)

            corperateID = Request.QueryString("CorperateID")
            corperateID = If(String.IsNullOrEmpty(corperateID), "", corperateID.Trim)
            taxID = Request.QueryString("TaxID")
            taxID = If(String.IsNullOrEmpty(taxID), "", taxID.Trim)
            EntityType = Request.QueryString("EntityType")
            EntityType = If(String.IsNullOrEmpty(EntityType), "", EntityType.Trim)
            OtherDocID = Request.QueryString("OtherDocID")
            OtherDocID = If(String.IsNullOrEmpty(OtherDocID), "", OtherDocID.Trim)


            'passportID = "A18476851|MWD060077"
            'uniqueID1 = "3560300540191|6502053014789|8502084002509"
            'OtherDocID = "NPHJ79DD6|409"

            splitIDNum = uniqueID1.Split("|")
            splitIDPass = passportID.Split("|")
            splitCorpID = corperateID.Split("|")
            splitTaxIDNumber = taxID.Split("|")
            splitIDOtherDoc = OtherDocID.Split("|")

            '====Old Version======
            'hdfUniqueID = Request.QueryString("hdfUniqueID").Trim
            'uniqueID1 = Request.QueryString("uniqueID").Trim
            'TranType = Request.QueryString("TranType").Trim
            'UniqueType = Request.QueryString("UniqueType").Trim
            'PrefixTH = Request.QueryString("PrefixTH").Trim
            'NameTh1 = Request.QueryString("NameTh1").Trim.Replace("|", "&")
            'NameTh2 = Request.QueryString("NameTh2").Trim.Replace("|", "&")
            'NameTh3 = Request.QueryString("NameTh3").Trim.Replace("|", "&")
            'PrefixEN = Request.QueryString("PrefixEN").Trim
            'NameEn1 = Request.QueryString("NameEn1").Trim.Replace("|", "&")
            'NameEn2 = Request.QueryString("NameEn2").Trim.Replace("|", "&")
            'NameEn3 = Request.QueryString("NameEn3").Trim.Replace("|", "&")
            'Nationality = Request.QueryString("Nationality").Trim
            'Occupation1 = Request.QueryString("Occupation1").Trim
            'OccOther1 = Request.QueryString("OccOther1").Trim
            'Business1 = Request.QueryString("Business1").Trim
            'BizOther1 = Request.QueryString("BizOther1").Trim
            'Des1 = Request.QueryString("Des1").Trim
            'Occupation2 = Request.QueryString("Occupation2").Trim
            'OccOther2 = Request.QueryString("OccOther2").Trim
            'Business2 = Request.QueryString("Business2").Trim
            'BizOther2 = Request.QueryString("BizOther2").Trim
            'Des2 = Request.QueryString("Des2").Trim
            'Occupation3 = Request.QueryString("Occupation3").Trim
            'OccOther3 = Request.QueryString("OccOther3").Trim
            'Business3 = Request.QueryString("Business3").Trim
            'BizOther3 = Request.QueryString("BizOther3").Trim
            'Des3 = Request.QueryString("Des3").Trim
            'IncomeSource1 = Request.QueryString("IncomeSource1").Trim
            'IncomeSource2 = Request.QueryString("IncomeSource2").Trim
            'IncomeSource3 = Request.QueryString("IncomeSource3").Trim
            'TypeKYC = Request.QueryString("TypeKYC").Trim
            'CustSeq = Request.QueryString("CustSeq").Trim
        Catch ex As Exception
            Throw ex
            WriteError(ex, "QueryString", "AMLList")
        End Try
    End Sub

    Private Function checkBeforeCompile() As Boolean
        Try
            clsCheckID = New clsCheckID
            clsCheckLanguage = New clsCheckLanguage

            Dim result As Boolean = True

            'If TranType = "" Then
            '    result = False
            '    GoTo chkresult
            'End If

            ' === Old version ==== 
            'If UniqueType = "1" Then

            '    If Not uniqueID1.Trim.Length = 13 Then
            '        result = False
            '        GoTo chkresult
            '    Else
            '        If Not uniqueID1 = hdfUniqueID Then
            '            If Not (clsCheckID.chkIDcard(uniqueID1.Trim)) Then
            '                result = False
            '                GoTo chkresult
            '            ElseIf clsCheckID.checkUniqueID1(uniqueID1, Session("TypeKYC"), Session("CustSeq")) = False Then
            '                result = False
            '                clsCheckID = Nothing
            '                GoTo chkresult

            '            End If
            '        End If

            '    End If
            'Else
            '    If uniqueID1.Trim.Length = 0 Then
            '        result = False
            '        GoTo chkresult
            '    ElseIf clsCheckID.checkUniqueID1(uniqueID1, Session("TypeKYC"), Session("CustSeq")) = False Then
            '        result = False
            '        clsCheckID = Nothing
            '        GoTo chkresult

            '    End If
            'End If


            ' === New Version : Ball V-Smart=====
            Dim fault As Integer = 0

            For Each idNum In splitIDNum
                If Not idNum.Trim.Length = 13 Then
                    If idNum.Trim.Length > 1 Then
                        ' fatal fault
                        fault = 4
                        msgErr = "เลขที่บัตรประชาชนต้องมี 13 หลัก"
                    End If

                    result = False
                    'GoTo chkresult
                    fault += 1
                Else
                    'If Not uniqueID1 = hdfUniqueID Then
                    If Not (clsCheckID.chkIDcard(idNum.Trim)) Then
                        ' fatal fault
                        result = False
                        fault = 4
                        msgErr = "เลขที่บัตรประชาชนไม่ถูกต้อง"
                        'GoTo chkresult
                        'ElseIf clsCheckID.checkUniqueID1(uniqueID1, Session("TypeKYC"), Session("CustSeq")) = False Then
                        '    result = False
                        '    clsCheckID = Nothing
                        '    GoTo chkresult

                    End If
                    clsCheckID = New clsCheckID
                    'End If
                End If
            Next



            If String.IsNullOrEmpty(passportID.Trim) Then
                result = False
                fault += 1
                'GoTo chkresult
            End If


            'If Nationality = "202" Then
            If NameTh2 = "" AndAlso NameTh1 = "" AndAlso NameTh3 = "" Then
                result = False
                fault += 1
                'GoTo chkresult
                'ElseIf Not PrefixTH = "" And Not NameTh1 = "" And Not NameTh3 = "" Then

            Else
                If clsCheckLanguage.checkLanguageTH(NameTh1, NameTh2, NameTh3) = False Then
                    '        result = False
                    '        GoTo chkresult
                    '    End If
                    'Else
                    '    If PrefixTH = "" Then
                    '        result = False
                    '        GoTo chkresult
                    '    End If

                    '    If NameTh1 = "" Then
                    '        result = False
                    '        GoTo chkresult
                    '    End If

                    '    If NameTh3 = "" Then
                    '        result = False
                    '        GoTo chkresult
                    '    End If
                    ' fatal fault
                    fault = 4
                    msgErr = "ชื่อภาษาไทยต้องเป็นตัวอักษรภาษาไทยเท่านั้น"
                End If

            End If


TH:
            ' === Old Version
            'If PrefixEN = "" Or NameEn1 = "" Or NameEn3 = "" Then
            '    result = False
            '    GoTo chkresult
            ' === new Version
            If NameEn2 = "" AndAlso NameEn1 = "" AndAlso NameEn3 = "" Then
                result = False
                fault += 1
                'GoTo chkresult
                'ElseIf Not PrefixTH = "" And Not NameTh1 = "" And Not NameTh3 = "" Then

            Else
                If clsCheckLanguage.checkLanguageEN(NameEn1, NameEn2, NameEn3) = False Then
                    '        result = False
                    '        GoTo chkresult
                    '    End If
                    'Else
                    '    If PrefixTH = "" Then
                    '        result = False
                    '        GoTo chkresult
                    '    End If

                    '    If NameTh1 = "" Then
                    '        result = False
                    '        GoTo chkresult
                    '    End If

                    '    If NameTh3 = "" Then
                    '        result = False
                    '        GoTo chkresult
                    '    End If
                    ' fatal fault
                    msgErr = "ชื่อภาษาอังกฤษต้องเป็นตัวอักษรภาษาอังกฤษเท่านั้น"
                    fault = 4
                End If

            End If

EN:
            'If Nationality = "" Then
            '    result = False
            '    GoTo chkresult
            'End If

            'If Occupation1 = "" Then
            '    result = False
            '    GoTo chkresult
            'Else
            '    If Not Occupation1 = "" Then
            '        If Occupation1 = 98 Then 'เจ้าของ
            '            If Business1 = "0" Then
            '                result = False
            '                GoTo chkresult
            '            ElseIf Business1 = 99 Then
            '                If BizOther1 = "" Then
            '                    result = False
            '                    GoTo chkresult
            '                End If
            '            End If
            '        ElseIf Occupation1 = 99 Then 'อื่นๆ
            '            If OccOther1 = "" Then
            '                result = False
            '                GoTo chkresult
            '            End If
            '        End If
            '    End If

            '    If Not Occupation2 = "" Then
            '        If Occupation2 = 98 Then 'เจ้าของ
            '            If Business2 = "0" Then
            '                result = False
            '                GoTo chkresult
            '            ElseIf Business2 = 99 Then
            '                If BizOther2 = "" Then
            '                    result = False
            '                    GoTo chkresult
            '                End If
            '            End If
            '        ElseIf Occupation2 = 99 Then 'อื่นๆ
            '            If OccOther2 = "" Then
            '                result = False
            '                GoTo chkresult
            '            End If
            '        End If
            '    End If

            '    If Not Occupation3 = "" Then
            '        If Occupation3 = 98 Then 'เจ้าของ
            '            If Business3 = "0" Then
            '                result = False
            '                GoTo chkresult
            '            ElseIf Business3 = 99 Then
            '                If BizOther3 = "" Then
            '                    result = False
            '                    GoTo chkresult
            '                End If
            '            End If
            '        ElseIf Occupation3 = 99 Then 'อื่นๆ
            '            If OccOther3 = "" Then
            '                result = False
            '                GoTo chkresult
            '            End If
            '        End If
            '    End If
            'End If


            'If IncomeSource1 = "" Then
            '    result = False
            '    GoTo chkresult
            'End If

            clsCheckLanguage = Nothing

chkresult:
            If fault > 3 Then
                If String.IsNullOrEmpty(msgErr) Then
                    msgErr = "ต้องกรอกเลขที่บัตรประชาชน/หนังสือเดินทาง ชื่อไทย ชื่ออังกฤษ หรืออย่างใดอย่างหนึ่ง"
                End If
                Return False
            Else
                Return True
            End If


        Catch ex As Exception
            WriteError(ex, "checkBeforeCompile", "AMLList")
            'Throw ex
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                           UpdatePanel1.GetType(), _
                                                           "clientScript", _
                                                           "alert('" + ex.Message.Replace("'", "") + "')", _
                                                           True)
            Return False
        End Try
    End Function

    Private Sub AutoInput()
        Try
            If Not String.IsNullOrEmpty(EntityType) AndAlso EntityType.ToUpper.Equals("E") Then
                Label1.Text = "Corperate ID"
                Label7.Text = "TAXID"
                lblUniqueID.Text = corperateID
                lblPassport.Text = taxID

                Dim idCorpID4Show As String = Nothing
                For Each idCorp In splitCorpID
                    If Not String.IsNullOrEmpty(idCorpID4Show) Then
                        idCorpID4Show += "<br/>"
                    End If
                    idCorpID4Show += idCorp
                Next
                lblUniqueID.Text = idCorpID4Show

                Dim idTaxID4Show As String = Nothing
                For Each idTax In splitTaxIDNumber
                    If Not String.IsNullOrEmpty(idTaxID4Show) Then
                        idTaxID4Show += "<br/>"
                    End If
                    idTaxID4Show += idTax
                Next
                lblPassport.Text = idTaxID4Show
            Else
                'lblUniqueID.Text = uniqueID1
                Dim idUnique4Show As String = Nothing
                For Each idNum In splitIDNum
                    If Not String.IsNullOrEmpty(idUnique4Show) Then
                        idUnique4Show += "<br/>"
                    End If
                    idUnique4Show += idNum
                Next
                lblUniqueID.Text = idUnique4Show

                Dim idPass4Show As String = Nothing
                For Each idPass In splitIDPass
                    If Not String.IsNullOrEmpty(idPass4Show) Then
                        idPass4Show += "<br/>"
                    End If
                    idPass4Show += idPass
                Next
                lblPassport.Text = idPass4Show

                Dim idOtherDoc4Show As String = Nothing
                For Each idOther In splitIDOtherDoc
                    If Not String.IsNullOrEmpty(idOtherDoc4Show) Then
                        idOtherDoc4Show += "<br/>"
                    End If
                    idOtherDoc4Show += idOther
                Next
                lblOtherDoc.Text = idOtherDoc4Show
            End If
            lblTHName.Text = NameTh1 + "&nbsp;&nbsp;&nbsp;" + NameTh2 + "&nbsp;&nbsp;&nbsp;" + NameTh3
            lblENName.Text = (NameEn1.ToUpper + "&nbsp;&nbsp;&nbsp;" + NameEn2.ToUpper + "&nbsp;&nbsp;&nbsp;" + NameEn3.ToUpper)
        Catch ex As Exception
            WriteError(ex, "AutoInput", "AMLList")
            Throw ex
        End Try
    End Sub


    Private Sub QuerySanction()

        'ถึงอ้ำ
        'แก้โค๊ดส่วนี้ ให้ไปเรียก web service โดยผลลัพธ์ที่ได้ให้ใส่ใน dataset ชื่อเดิมที่ทำไว้ 
        'โดยให้ส่งค่าไป web service 4 รอบ คือ ชื่อ-สกุล, สกลุ-ชื่อ ทั้งภาษาไทย อังกฤษ
        'แล้วให้รวมผลลัพธ์ไว้ที่ dataset ตัวเดียวกัน แล้วโชว์ผลลัพธ์ที่ grid
        'ชื่อที่ส่งไปให้ตัดอักขระพิเศษก่อน ให้ไปก๊อปปี้ที่หน้า NonIndividualSearc ที่ฟังชันก์ cutSpecialCharacter
        '
        'แก้ที่ saveData ในหน้านี้ด้วย เป็นการบันทึกผลลัพธ์ ให้แก้ฟิวด์ที่บอกว่าทำชั่วคราว เป็นฟิวด์ที่บอกให้พี่หนุ่ยส่งว่างๆ กลับมา ทำเพื่อเป็นการบันทึกว่ามีการเลือกรายการไหนไว้บ้าง
        'หน้าจอ AMLList กับ AMLListBiz แก้เหมือนกัน ต่างกันที่ type ที่แยกระหว่างบุคคลกะนิติ

        Try


            callWSVToChkName(NameTh1.Trim, NameTh2.Trim, NameTh3.Trim, _
                             NameEn1.Trim, NameEn2.Trim, NameEn3.Trim)



            'qryList = Nothing
        Catch ex As Exception
            '  WriteError(ex, "QuerySanction", "AMLList")
            Throw ex
        End Try
    End Sub

    Private Sub QuerySanction2()
        Dim dsService As New DataSet()
        Dim clsLINQToDataTable As New clsLINQToDataTable


        Try
            If (ds.Tables.Contains("Result")) Then
                ds.Tables.Remove("Result")
            End If
            Dim IDNUM = uniqueID1
            'Dim IDNUM = 1006010555
            Dim IDPass = passportID
            Dim corpID = corperateID
            Dim taxNumber = taxID



            Dim entType = "P"

            Dim SanctionListAll As New ADOEntitySanctionList.SanctionListEntities
            Dim res = Nothing
            Dim chkParam As Boolean = False

            If Not String.IsNullOrEmpty(EntityType) AndAlso EntityType.ToUpper.Equals("E") Then
                If Not String.IsNullOrEmpty(corpID) Or Not String.IsNullOrEmpty(taxID) Then
                    entType = "E"
                    chkParam = True
                End If

            Else
                If IDNUM IsNot Nothing Or Not String.IsNullOrEmpty(IDPass) Then
                    chkParam = True
                End If
            End If

            If chkParam Then

                'res = "select distinct m.entity_id,m.info_source as dataSource," +
                '" m.last_update_date as LastUpdateDate, " +
                '"i.id_value as UniqueID, b.birthinfo_date, r.country_desc," +
                '"n.firstname_e, n.middle_name_e, n.surname_e, n.original_script_name, " +
                '"n.first_name, n.middle_name, n.surname, n.single_string_name as FullName ," +
                '"ms.risklevel " +
                '"from KYCADMIN.amlo_new_idnumber_temp i " +
                '"join KYCADMIN.amlo_new_master_temp m on i.entity_id = m.entity_id " +
                '"left join KYCADMIN.amlo_new_birthinfo_temp b on i.entity_id = b.entity_id " +
                '"left join KYCADMIN.amlo_new_resident_temp r on i.entity_id = r.entity_id " +
                '"left join KYCADMIN.amlo_new_name_temp n on i.entity_id = n.entity_id " +
                '"join KYCADMIN.amlo_new_import_temp im on im.importid = m.importid " +
                '"join KYCADMIN.msanctionlist ms on im.file_type = ms.listtypename " +
                '" where i.id_value is not null          "

                res = "select * from (" &
                 "select distinct m.entity_id,m.info_source as dataSource," +
               " m.last_update_date as LastUpdateDate, " +
               " i.id_value as UniqueID, " +
               "n.firstname_e, n.middle_name_e, n.surname_e, n.original_script_name, " +
                "n.first_name, n.middle_name, n.surname, n.single_string_name as FullName ," +
               " ms.risklevel , ms.listtypename,ms.listtypecode, " +
               " 1 as SELECTLIST , '' as BIRTHINFO_DATE , '' as COUNTRY_DESC ," +
               " ROW_NUMBER() OVER (PARTITION BY m.entity_id ORDER BY m.entity_id) AS RowNumber " &
               "from KYCADMIN.amlo_new_idnumber_temp i " +
               "join KYCADMIN.amlo_new_master_temp m on i.entity_id = m.entity_id " +
               "left join KYCADMIN.amlo_new_name_temp n on i.entity_id = n.entity_id " +
               "join KYCADMIN.amlo_new_import_temp im on im.importid = m.importid " +
               "join KYCADMIN.msanctionlist ms on im.file_type = ms.LISTTYPECODE " +
               " where i.id_value is not null          "


                Dim strParam As String = ""
                If entType.Equals("P") Then
                    res += "and upper(m.entity_type) = 'PERSON' "
                    'If Not String.IsNullOrEmpty(IDNUM) Then
                    '    strParam = "  i.id_value like '%" + IDNUM.ToString + "%' "
                    'End If

                    If Not String.IsNullOrEmpty(splitIDNum(0)) Then

                        For Each id As String In splitIDNum
                            If Not String.IsNullOrEmpty(strParam) Then
                                strParam += "or"
                            End If
                            strParam += "  i.id_value = '" + id + "' "
                        Next
                    End If

                    If Not String.IsNullOrEmpty(splitIDPass(0)) Then

                        For Each passport As String In splitIDPass
                            If Not String.IsNullOrEmpty(strParam) Then
                                strParam += "or"
                            End If
                            strParam += "  upper(i.id_value) like '%" + passport.ToUpper + "%' "
                        Next

                    End If

                ElseIf entType.Equals("E") Then
                    res += "and upper(m.entity_type) = 'PERSON' "

                    If Not String.IsNullOrEmpty(splitCorpID(0)) Then

                        For Each cID As String In splitCorpID
                            If Not String.IsNullOrEmpty(strParam) Then
                                strParam += "or"
                            End If
                            strParam += "  upper(i.id_value) like '%" + cID.ToUpper + "%' "
                        Next
                    End If

                    If Not String.IsNullOrEmpty(splitTaxIDNumber(0)) Then

                        For Each tID As String In splitTaxIDNumber
                            If Not String.IsNullOrEmpty(strParam) Then
                                strParam += "or"
                            End If
                            strParam += "  upper(i.id_value) like '%" + tID.ToUpper + "%' "
                        Next
                    End If

                    'If Not String.IsNullOrEmpty(taxNumber) Then
                    '    If Not String.IsNullOrEmpty(strParam) Then
                    '        strParam += "or"
                    '    End If
                    '    strParam += "  i.id_value like '%" + taxNumber.ToString + "%'  "
                    'End If


                End If

                If Not String.IsNullOrEmpty(splitIDOtherDoc(0)) Then

                    For Each oDoc As String In splitIDOtherDoc
                        If Not String.IsNullOrEmpty(strParam) Then
                            strParam += "or"
                        End If
                        strParam += "  upper(i.id_value) like '%" + oDoc.ToUpper + "%' "
                    Next

                End If

                If Not String.IsNullOrEmpty(strParam) Then
                    strParam = " and  ( " + strParam + " )"
                    res += strParam
                    res += "order by  m.entity_id" &
                    " ) where RowNumber = 1 "
                Else
                    res = ""
                End If
            End If



            Dim res2 As String = Nothing

            'Dim isHaveParamName = False
            'Dim strBuilder As New StringBuilder()
            'Dim strParamAll As String = ""

            'If entType.Equals("P") Then

            '    Dim strParam1 As String = ""
            '    Dim strParam2 As String = ""

            '    If Not String.IsNullOrEmpty(NameEn1) Then

            '        strParam1 = " n.firstname_e like '%" + NameEn1 + "%'  "
            '    End If
            '    If Not String.IsNullOrEmpty(NameEn2) Then
            '        If Not String.IsNullOrEmpty(strParam1) Then
            '            strParam1 = strParam1 & " and "
            '        End If
            '        strParam1 = strParam1 & "  n.middle_name_e like '%" + NameEn2 + "%'  "

            '    End If
            '    If Not String.IsNullOrEmpty(NameEn3) Then
            '        If Not String.IsNullOrEmpty(strParam1) Then
            '            strParam1 = strParam1 & " and "
            '        End If
            '        strParam1 = strParam1 & "  n.surname_e like '%" + NameEn3 + "%'  "
            '    End If


            '    If Not String.IsNullOrEmpty(strParam1) Then
            '        strParam1 = " ( " & strParam1


            '        strParam1 = strParam1 & " ) "

            '    End If

            '    If Not String.IsNullOrEmpty(NameTh1) Then

            '        strParam2 = "  n.FIRST_NAME like '%" + NameTh1 + "%'"
            '    End If
            '    If Not String.IsNullOrEmpty(NameTh2) Then
            '        If Not String.IsNullOrEmpty(strParam2) Then
            '            strParam2 = strParam2 & " and "
            '        End If
            '        strParam2 = strParam2 & "  n.MIDDLE_NAME like '%" + NameTh2 + "%'"

            '    End If
            '    If Not String.IsNullOrEmpty(NameTh3) Then
            '        If Not String.IsNullOrEmpty(strParam2) Then
            '            strParam2 = strParam2 & " and "
            '        End If
            '        strParam2 = strParam2 & "  n.SURNAME like '%" + NameTh3 + "%'"
            '    End If


            '    If Not String.IsNullOrEmpty(strParam1) Then
            '        strParam2 = " ( " & strParam2


            '        strParam2 = strParam2 & " ) "

            '    End If

            '    If Not String.IsNullOrEmpty(strParam1) AndAlso Not String.IsNullOrEmpty(strParam2) Then
            '        strParamAll = " and ( " & strParam1 & "or" & strParam2 & " )"
            '        isHaveParamName = True
            '    Else
            '        If Not String.IsNullOrEmpty(strParam1) Then
            '            strParamAll = " and " & strParam1
            '            isHaveParamName = True
            '        ElseIf Not String.IsNullOrEmpty(strParam2) Then
            '            strParamAll = " and " & strParam2
            '            isHaveParamName = True
            '        End If
            '    End If
            'ElseIf entType.Equals("E") Then

            '    If Not String.IsNullOrEmpty(NameTh1) Then 'Or Not String.IsNullOrEmpty(NameEn1)
            '        strParamAll = "  n.ENTITY_NAME like '%" + NameTh1 + "%'"
            '    End If
            '    If Not String.IsNullOrEmpty(NameEn1) Then
            '        If Not String.IsNullOrEmpty(strParamAll) Then
            '            strParamAll = strParamAll & " or "
            '        End If
            '        strParamAll = strParamAll & "  n.ENTITY_NAME_E like '%" + NameEn1 + "%'"
            '    End If

            '    If Not String.IsNullOrEmpty(strParamAll) Then
            '        isHaveParamName = True
            '        strParamAll = " and ( " & strParamAll & " ) "
            '    End If

            'End If
            'strBuilder.Append(strParamAll)



            'If isHaveParamName Then

            '    res2 = " select * from ( " &
            '    "select distinct m.INFO_SOURCE as dataSource, m.ENTITY_ID, i.ID_VALUE as UniqueID,n.original_script_name," +
            '    " n.single_string_name as FullName,m.LAST_UPDATE_DATE as LastUpdateDate,ms.risklevel, ms.listtypename,ms.listtypecode, " +
            '    " 1 as SELECTLIST , " +
            '    " ROW_NUMBER() OVER (PARTITION BY m.entity_id  ORDER BY m.entity_id) AS RowNumber " &
            '    " from  KYCADMIN.amlo_new_master_temp m  " +
            '    " left join KYCADMIN.amlo_new_idnumber_temp i on m.entity_id = i.entity_id " +
            '    " join KYCADMIN.amlo_new_name_temp n  on m.entity_id = n.entity_id " +
            '    " join KYCADMIN.amlo_new_import_temp im on im.importid = m.importid " +
            '    " join KYCADMIN.msanctionlist ms on im.file_type = ms.LISTTYPECODE "

            '    If Not String.IsNullOrEmpty(entType) And entType.ToUpper.Equals("E") Then
            '        res2 = res2 & " where upper(m.entity_type) = 'ENTITY'"
            '    Else
            '        res2 = res2 & " where upper(m.entity_type) = 'PERSON'"
            '    End If


            '    res2 = res2 & strBuilder.ToString
            '    res2 = res2 & " order by  m.entity_id"
            '    res2 = res2 & ") where RowNumber = 1 "

            'End If
            Dim dsFuzzyEnSet = Nothing
            Dim dsFuzzyTHSet = Nothing
            If Not String.IsNullOrEmpty(NameEn1) Or Not String.IsNullOrEmpty(NameEn3) Or Not String.IsNullOrEmpty(NameEn2) Then
                dsFuzzyEnSet = callWSVFuzzyEN()
            End If
            If Not String.IsNullOrEmpty(NameTh1) Or Not String.IsNullOrEmpty(NameTh3) Or Not String.IsNullOrEmpty(NameTh2) Then
                dsFuzzyTHSet = callWSVFuzzyTH()
            End If

            Dim dtSanction As DataTable = Nothing
            If Not String.IsNullOrEmpty(res) Then
                dtSanction = clsDatabase.QueryDataTable(res)
                dtSanction.TableName = "Result"

                dsService.Tables.Add(dtSanction)

                Dim column = New DataColumn
                With column
                    .ColumnName = "TYPE"
                    .DataType = System.Type.GetType("System.Decimal")
                    .DefaultValue = 0
                    .Unique = False
                End With

                dsService.Tables("Result").Columns.Add(column)
                dsService.AcceptChanges()

                For iCountDtTable = 0 To dtSanction.Rows.Count - 1
                    dtSanction.Rows(iCountDtTable)("TYPE") = -1
                Next
            End If


            'If Not String.IsNullOrEmpty(res2) Then
            '    'Dim dtSanctionFromSAS As DataTable = clsLINQToDataTable.LINQToDataTable(res2, "Result")
            '    Dim dtSanctionFromSAS As DataTable = clsDatabase.QueryDataTable(res2)
            '    dtSanctionFromSAS.TableName = "Result"
            '    Dim column = New DataColumn
            '    With column
            '        .ColumnName = "TYPE"
            '        .DataType = System.Type.GetType("System.Decimal")
            '        .DefaultValue = 0
            '        .Unique = False
            '    End With
            '    dtSanctionFromSAS.Columns.Add(column)

            '    If Not String.IsNullOrEmpty(res) AndAlso dsService.Tables("Result").Rows.Count > 0 Then
            '        For iCountDtTable = 0 To dtSanctionFromSAS.Rows.Count - 1
            '            dsService.Tables("Result").ImportRow(dtSanctionFromSAS(iCountDtTable))
            '        Next
            '    Else
            '        dsService.Reset()
            '        dsService.Tables.Add(dtSanctionFromSAS)
            '    End If
            'End If

            'If res Is Nothing AndAlso res2 Is Nothing Then
            '    ds.Tables.Add(New DataTable("Result"))
            '    Return
            'End If

            Dim res3 = ""
            Dim dtFuzzyEN As DataTable = Nothing
            Dim dtFuzzyTH As DataTable = Nothing
            If dsFuzzyEnSet IsNot Nothing Then
                res2 = buildQueryForWSV(dsFuzzyEnSet)
                dtFuzzyEN = buildFuzzyDataSetFromQuery(res2, 0)
                If Not String.IsNullOrEmpty(res) AndAlso dsService.Tables("Result").Rows.Count > 0 Then
                    For iCountDtTable = 0 To dtFuzzyEN.Rows.Count - 1
                        dsService.Tables("Result").ImportRow(dtFuzzyEN(iCountDtTable))
                    Next
                Else
                    dsService.Reset()
                    dsService.Tables.Add(dtFuzzyEN)
                End If
                dsService.AcceptChanges()
            End If
            If dsFuzzyTHSet IsNot Nothing Then
                res3 = buildQueryForWSV(dsFuzzyTHSet)
                dtFuzzyTH = buildFuzzyDataSetFromQuery(res3, 1)
                If Not dsService.Tables("Result") Is Nothing AndAlso dsService.Tables("Result").Rows.Count > 0 Then
                    For iCountDtTable = 0 To dtFuzzyTH.Rows.Count - 1
                        dsService.Tables("Result").ImportRow(dtFuzzyTH(iCountDtTable))
                    Next
                Else
                    dsService.Reset()
                    dsService.Tables.Add(dtFuzzyTH)
                End If
                dsService.AcceptChanges()
            End If

            If String.IsNullOrEmpty(res) AndAlso dtFuzzyEN Is Nothing AndAlso dtFuzzyTH Is Nothing Then
                ds.Tables.Add(New DataTable("Result"))
                Return
            End If



            ds.Tables.Add(dsService.Tables("Result").Copy)

            If ds.Tables("Result").Rows.Count > 0 Then
                Dim maxRisk As Decimal = 1
                For Each row As DataRow In ds.Tables("Result").Rows
                    Dim riskLv = CDec(row("RiskLevel"))
                    If maxRisk < riskLv Then
                        maxRisk = riskLv
                    End If
                Next
                riskLv = maxRisk
            End If
            If Not String.IsNullOrEmpty(riskLv) Then
                hdfRiskLv.Value = riskLv
            End If

            grdList.DataSource = ds.Tables("Result")
            grdList.DataBind()

            ' Fill Grid Bg Color
            For iCountBegin As Integer = 0 To ds.Tables("Result").Rows.Count - 1
                If CInt(ds.Tables("Result").Rows(iCountBegin)("TYPE")) = -1 Then
                    ' Match by Identified Number 
                    Dim arrColor() As Integer = {255, 187, 255}
                    setGridRowBackground(grdList, iCountBegin, arrColor)

                ElseIf CInt(ds.Tables("Result").Rows(iCountBegin)("TYPE")) = 0 Then
                    Dim arrColor() As Integer = {255, 222, 173}
                    setGridRowBackground(grdList, iCountBegin, arrColor)

                Else
                    Dim arrColor() As Integer = {152, 251, 152}
                    setGridRowBackground(grdList, iCountBegin, arrColor)

                End If
                'If (iCountBegin >= 0 And iCountBegin < intCountRowTh) Then
                '    Dim arrColor() As Integer = {152, 251, 152}

                '    setGridRowBackground(grdList, iCountBegin, arrColor)
                'End If
                'If (iCountBegin >= intCountRowTh And iCountBegin < (intCountRowTh + intCountRowEn)) Then
                '    Dim arrColor() As Integer = {255, 222, 173}

                '    setGridRowBackground(grdList, iCountBegin, arrColor)
                'End If
                'If (iCountBegin >= (intCountRowTh + intCountRowEn) And iCountBegin < (intCountRowTh + intCountRowEn + intCountRowUniqueID)) Then
                '    Dim arrColor() As Integer = {255, 187, 255}

                '    setGridRowBackground(grdList, iCountBegin, arrColor)
                'End If
            Next

            Session("ds") = ds

        Catch ex As Exception
            WriteError(ex, "Query From TempAMLO Fail " & ex.Message, "AMLList")
            Throw ex
        End Try


    End Sub

    Private Sub QuerySanction3()
        Dim dsService As New DataSet()
        Dim clsLINQToDataTable As New clsLINQToDataTable


        Try
            If (ds.Tables.Contains("Result")) Then
                ds.Tables.Remove("Result")
            End If
            Dim IDNUM = uniqueID1
            'Dim IDNUM = 1006010555
            Dim IDPass = passportID
            Dim corpID = corperateID
            Dim taxNumber = taxID



            Dim entType = "P"

            Dim SanctionListAll As New ADOEntitySanctionList.SanctionListEntities
            Dim res = Nothing
            Dim chkParam As Boolean = False

            If Not String.IsNullOrEmpty(EntityType) AndAlso EntityType.ToUpper.Equals("E") Then
                If Not String.IsNullOrEmpty(corpID) Or Not String.IsNullOrEmpty(taxID) Then
                    entType = "E"
                    chkParam = True
                End If

            Else
                If IDNUM IsNot Nothing Or Not String.IsNullOrEmpty(IDPass) Then
                    chkParam = True
                End If
            End If

            If chkParam Then


                res = "select * from (" &
                 "select distinct m.entity_id,m.info_source as dataSource," +
               " m.last_update_date as LastUpdateDate, " +
               " i.id_value as UniqueID, " +
               "n.firstname_e, n.middle_name_e, n.surname_e, n.original_script_name, " +
                "n.first_name, n.middle_name, n.surname, n.single_string_name as FullName ," +
               " ms.risklevel , ms.listtypename,ms.listtypecode, " +
               " 1 as SELECTLIST , '' as BIRTHINFO_DATE , '' as COUNTRY_DESC ," +
               " ROW_NUMBER() OVER (PARTITION BY m.entity_id ORDER BY m.entity_id) AS RowNumber " &
               "from KYCADMIN.amlo_new_idnumber_temp i " +
               "join KYCADMIN.amlo_new_master_temp m on i.entity_id = m.entity_id " +
               "left join KYCADMIN.amlo_new_name_temp n on i.entity_id = n.entity_id " +
               "join KYCADMIN.amlo_new_import_temp im on im.importid = m.importid " +
               "join KYCADMIN.msanctionlist ms on im.file_type = ms.LISTTYPECODE " +
               " where i.id_value is not null          "


                Dim strParam As String = ""
                If entType.Equals("P") Then
                    res += "and upper(m.entity_type) = 'PERSON' "
                    'If Not String.IsNullOrEmpty(IDNUM) Then
                    '    strParam = "  i.id_value like '%" + IDNUM.ToString + "%' "
                    'End If

                    If Not String.IsNullOrEmpty(splitIDNum(0)) Then

                        For Each id As String In splitIDNum
                            If Not String.IsNullOrEmpty(strParam) Then
                                strParam += "or"
                            End If
                            strParam += "  i.id_value = '" + id + "' "
                        Next
                    End If

                    If Not String.IsNullOrEmpty(splitIDPass(0)) Then

                        For Each passport As String In splitIDPass
                            If Not String.IsNullOrEmpty(strParam) Then
                                strParam += "or"
                            End If
                            strParam += "  upper(i.id_value) like '%" + passport.ToUpper + "%' "
                        Next

                    End If

                ElseIf entType.Equals("E") Then
                    res += "and upper(m.entity_type) = 'PERSON' "

                    If Not String.IsNullOrEmpty(splitCorpID(0)) Then

                        For Each cID As String In splitCorpID
                            If Not String.IsNullOrEmpty(strParam) Then
                                strParam += "or"
                            End If
                            strParam += "  upper(i.id_value) like '%" + cID.ToUpper + "%' "
                        Next
                    End If

                    If Not String.IsNullOrEmpty(splitTaxIDNumber(0)) Then

                        For Each tID As String In splitTaxIDNumber
                            If Not String.IsNullOrEmpty(strParam) Then
                                strParam += "or"
                            End If
                            strParam += "  upper(i.id_value) like '%" + tID.ToUpper + "%' "
                        Next
                    End If

                End If

                If Not String.IsNullOrEmpty(splitIDOtherDoc(0)) Then

                    For Each oDoc As String In splitIDOtherDoc
                        If Not String.IsNullOrEmpty(strParam) Then
                            strParam += "or"
                        End If
                        strParam += "  upper(i.id_value) like '%" + oDoc.ToUpper + "%' "
                    Next

                End If

                If Not String.IsNullOrEmpty(strParam) Then
                    strParam = " and  ( " + strParam + " )"
                    res += strParam
                    res += "order by  m.entity_id" &
                    " ) where RowNumber = 1 "
                Else
                    res = ""
                End If
            End If

            Dim dtSanction As DataTable = Nothing
            If Not String.IsNullOrEmpty(res) Then
                dtSanction = clsDatabase.QueryDataTable(res)
                dtSanction.TableName = "Result"

                dsService.Tables.Add(dtSanction)

                Dim column = New DataColumn
                With column
                    .ColumnName = "TYPE"
                    .DataType = System.Type.GetType("System.Decimal")
                    .DefaultValue = 0
                    .Unique = False
                End With

                dsService.Tables("Result").Columns.Add(column)
                dsService.AcceptChanges()

                For iCountDtTable = 0 To dtSanction.Rows.Count - 1
                    dtSanction.Rows(iCountDtTable)("TYPE") = -1
                Next
            End If

            'If Not String.IsNullOrEmpty(res2) Then
            '    'Dim dtSanctionFromSAS As DataTable = clsLINQToDataTable.LINQToDataTable(res2, "Result")
            '    Dim dtSanctionFromSAS As DataTable = clsDatabase.QueryDataTable(res2)
            '    dtSanctionFromSAS.TableName = "Result"
            '    Dim column = New DataColumn
            '    With column
            '        .ColumnName = "TYPE"
            '        .DataType = System.Type.GetType("System.Decimal")
            '        .DefaultValue = 0
            '        .Unique = False
            '    End With
            '    dtSanctionFromSAS.Columns.Add(column)

            Dim dtSanctionFromLikeMethodENG = querySanction3helper(0)
            Dim dtSanctionFromLikeMethodTHA = querySanction3helper(1)

            If dtSanctionFromLikeMethodENG IsNot Nothing Or dtSanctionFromLikeMethodTHA IsNot Nothing Then
                If dtSanctionFromLikeMethodENG IsNot Nothing Then
                    If Not String.IsNullOrEmpty(res) AndAlso dsService.Tables("Result").Rows.Count > 0 Then
                        For iCountDtTable = 0 To dtSanctionFromLikeMethodENG.Rows.Count - 1
                            dsService.Tables("Result").ImportRow(dtSanctionFromLikeMethodENG(iCountDtTable))
                        Next
                    Else
                        dsService.Reset()
                        dsService.Tables.Add(dtSanctionFromLikeMethodENG)
                    End If
                End If

                If dtSanctionFromLikeMethodTHA IsNot Nothing Then
                    If dsService.Tables("Result") IsNot Nothing AndAlso dsService.Tables("Result").Rows.Count > 0 Then
                        For iCountDtTable = 0 To dtSanctionFromLikeMethodTHA.Rows.Count - 1
                            dsService.Tables("Result").ImportRow(dtSanctionFromLikeMethodTHA(iCountDtTable))
                        Next
                    Else
                        dsService.Reset()
                        dsService.Tables.Add(dtSanctionFromLikeMethodTHA)
                    End If
                End If
            Else
                If String.IsNullOrEmpty(res) Then
                    ds.Tables.Add(New DataTable("Result"))
                    Return
                End If
            End If
            'End If

            ds.Tables.Add(dsService.Tables("Result").Copy)

            If ds.Tables("Result").Rows.Count > 0 Then
                Dim maxRisk As Decimal = 1
                For Each row As DataRow In ds.Tables("Result").Rows
                    Dim riskLv = CDec(row("RiskLevel"))
                    If maxRisk < riskLv Then
                        maxRisk = riskLv
                    End If
                Next
                riskLv = maxRisk
            End If
            If Not String.IsNullOrEmpty(riskLv) Then
                hdfRiskLv.Value = riskLv
            End If

            grdList.DataSource = ds.Tables("Result")
            grdList.DataBind()

            ' Fill Grid Bg Color
            For iCountBegin As Integer = 0 To ds.Tables("Result").Rows.Count - 1
                If CInt(ds.Tables("Result").Rows(iCountBegin)("TYPE")) = -1 Then
                    ' Match by Identified Number 
                    Dim arrColor() As Integer = {255, 187, 255}
                    setGridRowBackground(grdList, iCountBegin, arrColor)

                ElseIf CInt(ds.Tables("Result").Rows(iCountBegin)("TYPE")) = 0 Then
                    Dim arrColor() As Integer = {255, 222, 173}
                    setGridRowBackground(grdList, iCountBegin, arrColor)

                Else
                    Dim arrColor() As Integer = {152, 251, 152}
                    setGridRowBackground(grdList, iCountBegin, arrColor)

                End If
                'If (iCountBegin >= 0 And iCountBegin < intCountRowTh) Then
                '    Dim arrColor() As Integer = {152, 251, 152}

                '    setGridRowBackground(grdList, iCountBegin, arrColor)
                'End If
                'If (iCountBegin >= intCountRowTh And iCountBegin < (intCountRowTh + intCountRowEn)) Then
                '    Dim arrColor() As Integer = {255, 222, 173}

                '    setGridRowBackground(grdList, iCountBegin, arrColor)
                'End If
                'If (iCountBegin >= (intCountRowTh + intCountRowEn) And iCountBegin < (intCountRowTh + intCountRowEn + intCountRowUniqueID)) Then
                '    Dim arrColor() As Integer = {255, 187, 255}

                '    setGridRowBackground(grdList, iCountBegin, arrColor)
                'End If
            Next

            Session("ds") = ds

        Catch ex As Exception
            WriteError(ex, "Query From TempAMLO Fail " & ex.Message, "AMLList")
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="langType"> 0 = english  1 = thai </param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function querySanction3helper(ByVal langType As Integer) As DataTable
        Dim res2 As String = Nothing
        Dim isHaveParamName = False
        Dim strBuilder As New StringBuilder()
        Dim strParamAll As String = ""
        Dim dtSanctionLikeMethod As DataTable = Nothing

        If EntityType.Equals("P") Or String.IsNullOrEmpty(EntityType) Then

            Dim strParam1 As String = ""
            Dim strParam2 As String = ""

            If langType = 0 Then
                If Not String.IsNullOrEmpty(NameEn1) Then

                    strParam1 = " n.firstname_e like '%" + NameEn1 + "%'  "
                End If
                If Not String.IsNullOrEmpty(NameEn2) Then
                    If Not String.IsNullOrEmpty(strParam1) Then
                        strParam1 = strParam1 & " and "
                    End If
                    strParam1 = strParam1 & "  n.middle_name_e like '%" + NameEn2 + "%'  "

                End If
                If Not String.IsNullOrEmpty(NameEn3) Then
                    If Not String.IsNullOrEmpty(strParam1) Then
                        strParam1 = strParam1 & " and "
                    End If
                    strParam1 = strParam1 & "  n.surname_e like '%" + NameEn3 + "%'  "
                End If

                If Not String.IsNullOrEmpty(strParam1) Then
                    strParam1 = " ( " & strParam1


                    strParam1 = strParam1 & " ) "

                End If
            End If

            If langType = 1 Then
                If Not String.IsNullOrEmpty(NameTh1) Then

                    strParam2 = "  n.FIRST_NAME like '%" + NameTh1 + "%'"
                End If
                If Not String.IsNullOrEmpty(NameTh2) Then
                    If Not String.IsNullOrEmpty(strParam2) Then
                        strParam2 = strParam2 & " and "
                    End If
                    strParam2 = strParam2 & "  n.MIDDLE_NAME like '%" + NameTh2 + "%'"

                End If
                If Not String.IsNullOrEmpty(NameTh3) Then
                    If Not String.IsNullOrEmpty(strParam2) Then
                        strParam2 = strParam2 & " and "
                    End If
                    strParam2 = strParam2 & "  n.SURNAME like '%" + NameTh3 + "%'"
                End If


                If Not String.IsNullOrEmpty(strParam1) Then
                    strParam2 = " ( " & strParam2
                    strParam2 = strParam2 & " ) "

                End If
            End If

            If Not String.IsNullOrEmpty(strParam1) AndAlso Not String.IsNullOrEmpty(strParam2) Then
                strParamAll = " and ( " & strParam1 & "or" & strParam2 & " )"
                isHaveParamName = True
            Else
                If Not String.IsNullOrEmpty(strParam1) Then
                    strParamAll = " and " & strParam1
                    isHaveParamName = True
                ElseIf Not String.IsNullOrEmpty(strParam2) Then
                    strParamAll = " and " & strParam2
                    isHaveParamName = True
                End If
            End If
        ElseIf EntityType.Equals("E") Then

            If Not String.IsNullOrEmpty(NameTh1) Then 'Or Not String.IsNullOrEmpty(NameEn1)
                strParamAll = "  n.ENTITY_NAME like '%" + NameTh1 + "%'"
            End If
            If Not String.IsNullOrEmpty(NameEn1) Then
                If Not String.IsNullOrEmpty(strParamAll) Then
                    strParamAll = strParamAll & " or "
                End If
                strParamAll = strParamAll & "  n.ENTITY_NAME_E like '%" + NameEn1 + "%'"
            End If

            If Not String.IsNullOrEmpty(strParamAll) Then
                isHaveParamName = True
                strParamAll = " and ( " & strParamAll & " ) "
            End If

        End If
        strBuilder.Append(strParamAll)



        If isHaveParamName Then

            res2 = " select * from ( " &
            "select distinct m.INFO_SOURCE as dataSource, m.ENTITY_ID, i.ID_VALUE as UniqueID,n.original_script_name," +
            " n.single_string_name as FullName,m.LAST_UPDATE_DATE as LastUpdateDate,ms.risklevel, ms.listtypename,ms.listtypecode, " +
            " 1 as SELECTLIST , " +
            " ROW_NUMBER() OVER (PARTITION BY m.entity_id  ORDER BY m.entity_id) AS RowNumber " &
            " from  KYCADMIN.amlo_new_master_temp m  " +
            " left join KYCADMIN.amlo_new_idnumber_temp i on m.entity_id = i.entity_id " +
            " join KYCADMIN.amlo_new_name_temp n  on m.entity_id = n.entity_id " +
            " join KYCADMIN.amlo_new_import_temp im on im.importid = m.importid " +
            " join KYCADMIN.msanctionlist ms on im.file_type = ms.LISTTYPECODE "

            If Not String.IsNullOrEmpty(EntityType) And EntityType.ToUpper.Equals("E") Then
                res2 = res2 & " where upper(m.entity_type) = 'ENTITY'"
            Else
                res2 = res2 & " where upper(m.entity_type) = 'PERSON'"
            End If


            res2 = res2 & strBuilder.ToString
            res2 = res2 & " order by  m.entity_id"
            res2 = res2 & ") where RowNumber = 1 "

        End If


        If Not String.IsNullOrEmpty(res2) Then
            'Dim dtSanctionFromSAS As DataTable = clsLINQToDataTable.LINQToDataTable(res2, "Result")
            dtSanctionLikeMethod = clsDatabase.QueryDataTable(res2)
            dtSanctionLikeMethod.TableName = "Result"
            Dim column = New DataColumn
            With column
                .ColumnName = "TYPE"
                .DataType = System.Type.GetType("System.Decimal")
                .DefaultValue = langType
                .Unique = False
            End With
            dtSanctionLikeMethod.Columns.Add(column)
        End If

        Return dtSanctionLikeMethod
    End Function

    Private Function cutSpecialCharacter(ByVal bizName As String) As String
        Try
            Dim nameSearch As String = ""
            'nameSearch = bizName.Trim.Replace(" ", "").Replace("&", "").Replace("'", "").Replace("(", "").Replace(")", "").Replace(",", "").Replace("-", "").Replace(".", "").Replace("""", "")
            nameSearch = bizName.Trim.Replace("'", "").Replace("""", "")

            Return nameSearch
        Catch ex As Exception
            WriteError(ex, "cutSpecialCharacter", "AMLList")
            Throw ex
        End Try
    End Function


    Private Sub callWSVToChkName(ByVal strFirstNameTh As String, ByVal strMidNameth As String, ByVal strLastNameTh As String, _
                                 ByVal strFirstNameEn As String, ByVal strMidNameEn As String, ByVal strLastNameEn As String)

        Dim SanctionEntity As New SanctionEntities
        Dim clsLINQToDataTable As New clsLINQToDataTable
        Dim intCountRowTh As Integer = 0
        Dim intCountRowEn As Integer = 0
        Dim intCountRowUniqueID As Integer = 0
        Dim total As Integer = 0
        Dim icount As Integer = 0
        Dim drItem As DataRow()
        Dim dsService As New DataSet

        Dim RowNumber As Integer = 0

        Try
            RowNumber = 1
            If (ds.Tables.Contains("Result")) Then
                RowNumber = 2

                ds.Tables.Remove("Result")
            End If
            RowNumber = 3


            ' เป็นการดึง service เช็คชื่อภาษาไทย------------------------------------------------------------------------
            'Dim objCallDataTH As New wsvSearchTh.ProcDQsvrsvc_th
            'RowNumber = 4
            'Dim objPrmNameTH As New wsvSearchTh.Parameters
            'RowNumber = 5
            'With objPrmNameTH
            '    .firstName = cutSpecialCharacter(strFirstNameTh)
            '    .middleName = cutSpecialCharacter(strMidNameth)
            '    .lastName = cutSpecialCharacter(strLastNameTh)
            'End With
            'RowNumber = 6
            Dim doc As XmlDocument = New XmlDocument

            'doc.LoadXml(objCallDataTH.procDQsvrsvc_th(objPrmNameTH))
            'RowNumber = 7
            'dsService.ReadXml(New XmlNodeReader(doc))
            'RowNumber = 8
            'drItem = Nothing
            'drItem = dsService.Tables("Result").Select("FirstName=''")
            'RowNumber = 9
            'icount = drItem.Length
            'For i = 0 To icount - 1
            '    drItem(i).Delete()
            'Next
            'RowNumber = 10

            'dsService.Tables("Result").AcceptChanges()
            'RowNumber = 11
            'intCountRowTh = dsService.Tables("Result").Rows.Count
            'RowNumber = 12
            '--------------------------------------------------------------------------------------------------

            ' เป็นการดึง service เช็คชื่อภาษาอังกฤษ------------------------------------------------------------------------
            Dim objCallDataEN As New wsvSearchEn.ProcDQsvrsvc_en
            Dim objPrmNameEN As New wsvSearchEn.Parameters


            With (objPrmNameEN)
                .firstName = strFirstNameEn.ToUpper
                .middleName = strMidNameEn.ToUpper
                .lastName = strLastNameEn.ToUpper
            End With
            RowNumber = 13
            doc = New XmlDocument
            doc.LoadXml(objCallDataEN.procDQsvrsvc_en(objPrmNameEN))
            RowNumber = 14
            dsService.ReadXml(New XmlNodeReader(doc))
            RowNumber = 15
            drItem = Nothing
            drItem = dsService.Tables("Result").Select("FirstName=''")
            RowNumber = 16
            icount = drItem.Length
            For i = 0 To icount - 1
                drItem(i).Delete()
            Next
            RowNumber = 17
            dsService.Tables("Result").AcceptChanges()
            RowNumber = 18
            intCountRowEn = dsService.Tables("Result").Rows.Count - intCountRowTh


            '-----------------------------9---------------------------------------------------------------------
            'ทำการคิวรี ด้วย uniqueID
            RowNumber = 19
            Dim tblSaction = From tblAmlList In SanctionEntity.AMLLIST_THAI Where tblAmlList.UNIQUEID = uniqueID1 _
                             Select tblAmlList.UNIQUEID, tblAmlList.FIRSTNAME, tblAmlList.MIDDLENAME, _
                             tblAmlList.LASTNAME, tblAmlList.CUSTTYPE, FULLNAME = tblAmlList.FIRSTNAME & " " & tblAmlList.LASTNAME, tblAmlList.COUNTRY, tblAmlList.DATASOURCE, tblAmlList.RISKLEVEL, _
                             tblAmlList.LASTUPDATEDATE, SELECTLIST = "", tblAmlList.COMMENT1, tblAmlList.COMMENT2

            'dsService.Tables.Add(clsLINQToDataTable.LINQToDataTable(tblSaction, "Result"))
            'dsService.Tables("Result").Merge(clsLINQToDataTable.LINQToDataTable(tblSaction, "Result"))
            RowNumber = 20
            Dim dtSanction As DataTable = Nothing
            dtSanction = clsLINQToDataTable.LINQToDataTable(tblSaction, "Result")
            'Try
            RowNumber = 21
            For iCountDtTable = 0 To dtSanction.Rows.Count - 1
                dsService.Tables("Result").ImportRow(clsLINQToDataTable.LINQToDataTable(tblSaction, "Result")(iCountDtTable))
            Next
            RowNumber = 22
            'Catch ex As Exception

            'End Try

            intCountRowUniqueID = dsService.Tables("Result").Rows.Count - (intCountRowTh + intCountRowEn)
            total = intCountRowTh + intCountRowEn + intCountRowUniqueID
            RowNumber = 23

            ds.Tables.Add(dsService.Tables("result").Copy)
            RowNumber = 24
            grdList.DataSource = ds.Tables("Result")
            grdList.DataBind()
            RowNumber = 25
            For iCountBegin As Integer = 0 To total - 1
                If (iCountBegin >= 0 And iCountBegin < intCountRowTh) Then
                    Dim arrColor() As Integer = {152, 251, 152}
                    'setGridRowBackground(grdList, iCountBegin, Drawing.Color.Honeydew)
                    setGridRowBackground(grdList, iCountBegin, arrColor)
                End If
                If (iCountBegin >= intCountRowTh And iCountBegin < (intCountRowTh + intCountRowEn)) Then
                    Dim arrColor() As Integer = {255, 222, 173}
                    'setGridRowBackground(grdList, iCountBegin, Drawing.Color.PapayaWhip)
                    setGridRowBackground(grdList, iCountBegin, arrColor)
                End If
                If (iCountBegin >= (intCountRowTh + intCountRowEn) And iCountBegin < (intCountRowTh + intCountRowEn + intCountRowUniqueID)) Then
                    Dim arrColor() As Integer = {255, 187, 255}
                    'setGridRowBackground(grdList, iCountBegin, Drawing.Color.LavenderBlush)
                    setGridRowBackground(grdList, iCountBegin, arrColor)
                End If
            Next
            RowNumber = 26



        Catch ex As Exception
            WriteError(ex, "callWSVToChkName and row " & RowNumber.ToString(), "AMLList")
            Throw ex
        End Try
    End Sub

    Private Function callWSVFuzzyEN() As DataSet

        Try
            Dim objCallFuzzyEN As New procDQEN.ProcDQsvrsvc_en
            Dim objPrmFuzzyName As New procDQEN.Parameters

            With objPrmFuzzyName
                .firstName = cutSpecialCharacter(NameEn1)
                .middleName = cutSpecialCharacter(NameEn2)
                .lastName = cutSpecialCharacter(NameEn3) 'tilemsy tilemsi , tilemci
            End With

            Dim res = objCallFuzzyEN.procDQsvrsvc_en(objPrmFuzzyName)
            Dim doc As XmlDocument = New XmlDocument

            doc.LoadXml(res)
            Dim dsService As New DataSet
            dsService.ReadXml(New XmlNodeReader(doc))


            If Not dsService.Tables("result").Rows(0).IsNull("uniqueId") Then
                Return dsService
            Else
                Return Nothing
            End If

        Catch ex As Exception
            'WriteError(ex, "callWSVToChkName and row " & RowNumber.ToString(), "AMLList")
            WriteError(ex, "callWSVFuzzyEN error", "AMLList")
            Throw New Exception("Fuzzy ENG service error.")
            Return Nothing
        End Try

    End Function

    Private Function callWSVFuzzyTH() As DataSet
        Try
            Dim objCallFuzzyTH As New procDQTH.ProcDQsvrsvc_th
            Dim objPrmFuzzyTHName As New procDQTH.Parameters

            With objPrmFuzzyTHName
                .firstName = NameTh1 '"แสงทิด"
                .middleName = NameTh2
                .lastName = NameTh3 '"คติสมสกุน"
            End With

            Dim res = objCallFuzzyTH.procDQsvrsvc_th(objPrmFuzzyTHName)
            Dim doc = New XmlDocument

            doc.LoadXml(res)
            Dim dsService As New DataSet
            dsService.ReadXml(New XmlNodeReader(doc))

            Return dsService
        Catch ex As Exception
            WriteError(ex, "callWSVFuzzyTH error", "AMLList")
            Throw New Exception("Fuzzy THA service error.")
            Return Nothing
        End Try
    End Function

    Private Function buildQueryForWSV(ByVal dsResult As DataSet) As String
        Dim res3 = " select * from ( " &
               "select distinct m.INFO_SOURCE as dataSource, m.ENTITY_ID, i.ID_VALUE as UniqueID," +
               "n.firstname_e, n.middle_name_e, n.surname_e, n.original_script_name, " +
               "n.first_name, n.middle_name, n.surname, n.single_string_name as FullName ," +
               " m.LAST_UPDATE_DATE as LastUpdateDate,ms.risklevel, ms.listtypename,ms.listtypecode, " +
               " 1 as SELECTLIST  , '' as BIRTHINFO_DATE , '' as COUNTRY_DESC , " +
               " ROW_NUMBER() OVER (PARTITION BY m.entity_id  ORDER BY m.entity_id) AS RowNumber " &
               " from  KYCADMIN.amlo_new_master_temp m  " +
               " left join KYCADMIN.amlo_new_idnumber_temp i on m.entity_id = i.entity_id " +
               " join KYCADMIN.amlo_new_name_temp n  on m.entity_id = n.entity_id " +
               " join KYCADMIN.amlo_new_import_temp im on im.importid = m.importid " +
               " join KYCADMIN.msanctionlist ms on im.file_type = ms.LISTTYPECODE "

        If Not String.IsNullOrEmpty(EntityType) And EntityType.ToUpper.Equals("E") Then
            res3 += " where upper(m.entity_type) = 'ENTITY'"
        Else
            res3 += " where upper(m.entity_type) = 'PERSON'"
        End If

        Dim whereClause = "( n.SYS_ID = '{0}' and n.ENTITY_ID = '{1}' )"
        Dim res3Where = ""
        If dsResult IsNot Nothing Then
            Dim dtRes = dsResult.Tables("result")
            If dtRes IsNot Nothing AndAlso dtRes.Rows.Count > 0 Then
                For i = 0 To dtRes.Rows.Count - 1
                    If Not String.IsNullOrEmpty(res3Where) Then
                        res3Where += "or"
                    End If
                    res3Where += String.Format(whereClause, dtRes.Rows(i)("custType"), dtRes.Rows(i)("uniqueId"))
                Next
            End If
        End If

        If Not String.IsNullOrEmpty(res3Where) Then
            res3 += " and ( " & res3Where & " )"
        End If
        res3 = res3 & " order by  m.entity_id"
        res3 = res3 & ") where RowNumber = 1 "

        Return res3
    End Function

    Private Function buildFuzzyDataSetFromQuery(ByVal strQuery As String, ByVal typeFuzzySearch As Integer) As DataTable

        Dim dtSanctionFromSAS As DataTable = clsDatabase.QueryDataTable(strQuery)
        dtSanctionFromSAS.TableName = "Result"
        Dim column = New DataColumn
        With column
            .ColumnName = "TYPE"
            .DataType = System.Type.GetType("System.Decimal")
            .DefaultValue = typeFuzzySearch
            .Unique = False
        End With
        dtSanctionFromSAS.Columns.Add(column)

        Return dtSanctionFromSAS
    End Function

    Private Sub setGridRowBackground(ByVal Grid As GridView, ByVal intRow As Integer, ByVal colorBack As Drawing.Color)
        Try
            Grid.Rows(intRow).BackColor = colorBack
            'Grid.Rows(intRow).BackColor.FromArgb(,,)
        Catch ex As Exception
            WriteError(ex, "setGridRowBackground", "AMLList")
            Throw ex
        End Try
    End Sub

    Private Sub setGridRowBackground(ByVal Grid As GridView, ByVal intRow As Integer, ByVal colorBack As Array)
        Try
            'Grid.Rows(intRow).BackColor = colorBack
            Grid.Rows(intRow).BackColor = Drawing.Color.FromArgb(colorBack(0), colorBack(1), colorBack(2))
        Catch ex As Exception
            WriteError(ex, "setGridRowBackground", "AMLList")
            Throw ex
        End Try
    End Sub

    Private Function checkBeforeSave() As Boolean
        Try
            Dim result As Boolean = False
            If rdoSanction.SelectedIndex = 0 Then
                Dim chk As CheckBox
                For i As Integer = 0 To grdList.Rows.Count - 1
                    chk = CType(grdList.Rows(i).FindControl("chkList"), CheckBox)
                    If chk.Checked = True Then
                        result = True
                    End If
                Next
            Else
                result = True
            End If

            Return result
        Catch ex As Exception
            WriteError(ex, "checkBeforeSave", "AMLList")
            Throw ex
        End Try
    End Function

    Private Sub saveData()
        Try
            Dim chk As CheckBox
            For i As Integer = 0 To grdList.Rows.Count - 1
                chk = CType(grdList.Rows(i).FindControl("chkList"), CheckBox)
                If chk.Checked = True Then
                    ds.Tables("Result").Rows(i)("SELECTLIST") = "0" 'ทำชั่วคราว รอเปลี่ยนฟิลด์อีกที
                    If grdList.Rows(i).Cells(3).Text = "PEPs" Then
                        If Session("Sanction") = Nothing Then
                            Session("Sanction") = "- พบรายชื่อใน PEPs List"
                        Else
                            Session("Sanction") += ", " + "PEPs"
                        End If
                    Else
                        If Session("Sanction") = Nothing Then
                            Session("Sanction") = "- พบรายชื่อใน " + grdList.Rows(i).Cells(3).Text
                        Else
                            Session("Sanction") += ", " + grdList.Rows(i).Cells(3).Text
                        End If
                    End If
                Else
                    ds.Tables("Result").Rows(i)("SELECTLIST") = "1" 'ทำชั่วคราว รอเปลี่ยนฟิลด์อีกที

                End If
                ds.Tables("Result").AcceptChanges()
            Next

        Catch ex As Exception
            WriteError(ex, "saveData", "AMLList")
            Throw ex
        End Try
    End Sub

#End Region

#Region "Event Button"

    Protected Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Try

            If checkBeforeSave() = True Then
                saveData()
                'ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "clientScript", "window.close();", True)

                Dim ret As String = hdfRiskLv.Value 'ค่าที่ต้องการ return 
                ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "SendValueBack", "window.returnValue = '" + ret + "';self.close();", True)


            Else
                ScriptManager.RegisterStartupScript(btnOK, Page.GetType(), "alert", "alert('กรุณาเลือกรายชื่อในลิสต์')", True)
            End If
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

#End Region

    Protected Sub rdoSanction_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoSanction.SelectedIndexChanged
        If rdoSanction.SelectedIndex = 0 Then
            btnOK.Enabled = True
            grdList.Enabled = True
        Else
            btnOK.Enabled = True
            grdList.Enabled = False


            'Dim chkSelect As CheckBox = DirectCast(sender, CheckBox)
            'Dim row As GridViewRow = DirectCast(chkSelect.NamingContainer, GridViewRow)
            'Dim hdnCheck As HiddenField = DirectCast(row.Cells(0).FindControl("hiddenField1"), HiddenField)

            For rowIndex As Integer = 0 To grdList.Rows.Count - 1
                'If hdnCheck.Value.Equals(grdList.Rows(rowIndex).Cells(1).Text) Then
                Dim checkBox = DirectCast(grdList.Rows(rowIndex).Cells(0).FindControl("chkList"), CheckBox)
                checkBox.Checked = False
                'End If
            Next
        End If

    End Sub

    Public Sub checkbox_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) 'Handles checkbox.CheckedChanged
        'Dim box As CheckBox = DirectCast(sender, CheckBox)

        'If box.Checked = True Then
        '    MsgBox("checked!")
        'Else
        '    MsgBox("unchecked!")
        'End If
        Dim chkSelect As CheckBox = DirectCast(sender, CheckBox)
        Dim row As GridViewRow = DirectCast(chkSelect.NamingContainer, GridViewRow)
        Dim hdnCheck As HiddenField = DirectCast(row.Cells(0).FindControl("hiddenField1"), HiddenField)

        For rowIndex As Integer = 0 To grdList.Rows.Count - 1
            If hdnCheck.Value.Equals(grdList.Rows(rowIndex).Cells(1).Text) Then
                Dim checkBox = DirectCast(grdList.Rows(rowIndex).Cells(0).FindControl("chkList"), CheckBox)
                checkBox.Checked = chkSelect.Checked
            End If
        Next

    End Sub

    Protected Sub grdList_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdList.RowDataBound
        If (e.Row.RowType.ToString = "DataRow") Then
            Dim lblActiveDate As Label = CType(e.Row.FindControl("lblActiveDate"), Label)
            If Not (lblActiveDate.Text = "") Then
                convertDate = New clsDateFormat
                lblActiveDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, lblActiveDate.Text)
                convertDate = Nothing
            End If

            Dim linkDetail As HyperLink = CType(e.Row.FindControl("linkDetail"), HyperLink)

            'e.Row.Attributes.Add("onmouseover", "javascript:this.style.cursor = 'pointer';")
            'e.Row.Attributes.Add("ondblclick", "win = window.showModalDialog('frmshowDetail.aspx?RowIndexDetail=" & e.Row.RowIndex & "',null,'width=600,height=800','_blank');")
            linkDetail.Attributes.Add("onmouseover", "javascript:this.style.cursor = 'pointer';")
            'linkDetail.Attributes.Add("onclick", "win = window.showModalDialog('frmshowDetail.aspx?RowIndexDetail=" & e.Row.RowIndex & "',null,'width=600,height=800','_blank');")
            linkDetail.Attributes.Add("onclick", "win = window.showModalDialog('frmshowDetail.aspx?RowIndexDetail=" & e.Row.RowIndex & "',null,'dialogHeight:400px;dialogWidth:600px;status:no','_blank');")
        End If
    End Sub


    Public Sub WriteError(ByVal objEx As Exception, ByVal strFunctionName As String, ByVal strFromName As String)
        Try
            Dim clsSessionMenuRight As clsSessionMenuRight = Session("clsMenuRight")
            Dim cls As New clsWriteError
            cls.GetstrErrorMessage = objEx.Message
            cls.GetstrErrorException = If(objEx.InnerException Is Nothing, "", objEx.InnerException.Message)
            cls.GetstrFromName = strFromName
            cls.GetstrUserName = clsSessionMenuRight.getUserName
            cls.GetstrUserRight = clsSessionMenuRight.getUserRight
            cls.GetstrFunctionName = strFunctionName
            cls.CreateFileXml(clsSessionMenuRight.getBranchCode)

            clsSessionMenuRight = Nothing
            cls = Nothing
        Catch ex As Exception

        End Try

    End Sub

End Class
